package com.zhj.psi.web.controller;

import com.zhj.psi.core.config.PageRange;
import com.zhj.psi.core.mvc.AjaxResult;
import com.zhj.psi.core.mvc.JsonArg;
import com.zhj.psi.core.tools.DateUtils;
import com.zhj.psi.core.tools.PageHelper;
import com.zhj.psi.web.entity.Product;
import com.zhj.psi.web.repository.ProductDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zhuhuijun on 2017/9/4 20:45
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductDao productDao;

    @RequestMapping("/list")
    public AjaxResult list(@JsonArg("product") Product product, @JsonArg("pageRange") PageRange pageRange) {
        return new AjaxResult().with(productDao.findAll(PageHelper.buildPageRequest(pageRange)));
    }

    @RequestMapping("/findAll")
    public AjaxResult findAll() {
        return new AjaxResult().with(productDao.findAll());
    }
    @RequestMapping("/find")
    public AjaxResult find(@JsonArg("id") Long id) {
        return new AjaxResult().with(productDao.findOne(id));
    }

    @RequestMapping("/save")
    public AjaxResult save(@JsonArg("product") Product product) {
        product.setUpdateTime(DateUtils.getNowDate());
        productDao.save(product);
        return new AjaxResult();
    }

    @RequestMapping("/update")
    public AjaxResult update(@JsonArg("product") Product product) {
        productDao.saveAndFlush(product);
        return new AjaxResult();
    }

    @RequestMapping("/delete")
    public AjaxResult delete(@JsonArg("id") Long id) {
        productDao.delete(id);
        return new AjaxResult();
    }
}
